/**
 *
 */
interface State {
  aimDom: {
    style?: { [organName: string]: string };
    context?: { value: string; type: string };
    faPosition?: { top: number; left: number };
    [organName: string]: any;
  }; //目标DOM节点
  aimPath: {}[]; //目标路径
  aimAttrs: {
    context?: string;
    goto?: { type: string; value: string };
    [organName: string]: any;
  }; //目标属性
  aimBox: { attrs?: { class: string } }; //目标容器
  aimXY: {}; //鼠标与组件的偏移
  componentList: { [organName: string]: {} };
  seniorBoxList: {};
  //为了可以显示margin...
  colorDomStyle: {
    width: string;
    height: string;
    top: string;
    left: string;
    padding: string;
    border: string;
    margin: string;
  };
  judgeColorDom: boolean;
  domStack: {
    type: string;
    pageName: string;
    data: (string | number | {})[];
    path: {}[][];
  }[]; //存储栈
  domStackNum: number; //存储栈控制下标
  page?: {
    tree: {
      [organName: string]: [];
    };
  };
}

export default function store_ondragstart(
  state: State,
  {
    e,
    that,
  }: {
    e: {
      target: any;
      path: { className: string }[];
      offsetX: string;
      offsetY: string;
    };
    that: { $searchDom: Function; pageName: string };
  }
) {
  let type = e.target.getAttribute("data-type"); //自制组件名称
  let path; //原来的路径

  // 判断是否拖动的是文本节点
  if (e.target.nodeName == "#text") return;
  // 判断是不是自制组件
  if (!type) return;

  //不法分子
  if (["carouselItemImage"].includes(type)) return;

  path = e.path.slice(0, -6).reverse();

  // 判断是否有class名--是不是新组件
  if (!e.target.className.match(new RegExp(type + "[0-9]{13}"))) {
    //新组件--在组件列表中添加
    state.aimDom = JSON.parse(JSON.stringify(state.componentList[type]));
  } else {
    //旧组件
    //查找组件
    state.aimDom = that.$searchDom(
      state.page?.tree[that.pageName],
      path,
      false
    );
    // 判断是不是移动
    if (["button", "text", "img", "freeBox"].includes(type)) {
      let sPath: {}[] = [];
      path.forEach((element) => {
        sPath.push({
          className: element.className,
        });
      });
      state.aimPath = sPath; //组件的路径--用于组件移动
    }
  }

  //鼠标与组件的偏移--用于计算组件移动距离
  state.aimXY = [e.offsetX, e.offsetY];
}
